package com.cskaoyan.util;

import lombok.SneakyThrows;

import java.security.MessageDigest;

/**
 * @Name : Md5util.java
 * @Time : 2021/8/16 11:14
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
public class Md5utils {

    @SneakyThrows
    private static String getMd5(String content) {
        MessageDigest messageDigest = MessageDigest.getInstance("md5");
        byte[] contentBytes = content.getBytes();
        byte[] resultBytes = messageDigest.digest(contentBytes);
        StringBuffer resultBuffer = new StringBuffer();
        for (byte resultByte : resultBytes) {
            int temp = resultByte & 0xff;
            String s = Integer.toHexString(temp);
            if (s.length() == 1) {
                resultBuffer.append(0);
            }
            if (s.length() == 2) {
                resultBuffer.append("$");
            }
            resultBuffer.append(s);
        }
        return resultBuffer.toString();
    }

    public static String getMd5Result(String content) {
        String result = getMd5(getMd5(getMd5(content)));
        char[] chars = result.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == 'a') {
                chars[i] = (char) (chars[i] - 32);
            } else if (chars[i] == 'b') {
                chars[i] = 'i';
            } else if (chars[i] == 'c') {
                chars[i] = 'S';
            } else if (chars[i] > 'c' && chars[i] <= 'f') {
                chars[i] = 'N';
            }
        }

        return String.valueOf(chars);
    }

}
